package com.ruoyi.web.controller.tool;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.ConfigService;
import com.ruoyi.system.domain.CyAfterinvestigation;
import com.ruoyi.system.domain.CyQuestionRequirement;
import com.ruoyi.system.service.ICyAfterinvestigationService;
import com.ruoyi.system.service.ICyQuestionRequirementService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;


import java.util.ArrayList;
        import java.util.LinkedHashMap;
        import java.util.List;
        import java.util.Map;
        import org.springframework.web.bind.annotation.DeleteMapping;
        import org.springframework.web.bind.annotation.GetMapping;
        import org.springframework.web.bind.annotation.PathVariable;
        import org.springframework.web.bind.annotation.PostMapping;
        import org.springframework.web.bind.annotation.PutMapping;
        import org.springframework.web.bind.annotation.RequestBody;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RestController;
        import com.ruoyi.common.core.controller.BaseController;
        import com.ruoyi.common.core.domain.AjaxResult;
        import com.ruoyi.common.utils.StringUtils;
        import io.swagger.annotations.Api;
        import io.swagger.annotations.ApiImplicitParam;
        import io.swagger.annotations.ApiImplicitParams;
        import io.swagger.annotations.ApiModel;
        import io.swagger.annotations.ApiModelProperty;
        import io.swagger.annotations.ApiOperation;

/**
 * swagger 用户测试方法
 *
 * @author ruoyi
 */
@Api("燕茹的测试接口")
@RestController
@RequestMapping("/yanru/test")
public class YanruTestController extends BaseController
{

    @Autowired
    private ICyQuestionRequirementService cyQuestionRequirementService;

    @Autowired
    private ICyAfterinvestigationService cyAfterinvestigationService;//

    private final static Map<Integer, UserEntity> users = new LinkedHashMap<Integer, UserEntity>();
    {
        users.put(1, new UserEntity(1, "admin111111111", "11111111111111", "158828888888"));
        users.put(2, new UserEntity(2, "ry111111111111", "11112222", "15666666666"));
    }
//
//    @ApiOperation("获取用户列表")
//    @GetMapping("/list")
//    public AjaxResult userList()
//    {
//        List<UserEntity> userList = new ArrayList<UserEntity>(users.values());
//        return AjaxResult.success(userList);
//    }

    @ApiOperation("燕茹测试获取用户详细")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path", dataTypeClass = Integer.class)
    @GetMapping("/{userId}")
    public AjaxResult getUser(@PathVariable Integer userId)
    {
        if (!users.isEmpty() && users.containsKey(userId))
        {
            return AjaxResult.success(users.get(userId));
        }
        else
        {
            return error("用户不存在");
        }
    }


    @ApiOperation("燕茹测试保存用户问卷信息")
//    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path", dataTypeClass = Integer.class)
//    @GetMapping("/{userId}")
    @PostMapping("/saveQuestion")
    @ResponseBody
//    @RequestBody
//    @RequestMapping(value = "/saveQuestion")//, method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public AjaxResult saveQuestion(@RequestBody String jsonObject)//JSONObject jsonObject)
    {
        //@RequestBody String jsonObject)//
        logger.debug("收到用户数据："+jsonObject);
//        JSONObject json = JSONObject.parseObject(jsonObject);
        //JSONObject jo = JSONObject.parseObject(jsonObject);
        logger.debug("收到用户数据2："+jsonObject);


        JSONObject obj =  JSON.parseObject(jsonObject);
        logger.debug("收到用户数据3："+obj);
        //数据已经转化

//        if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId()))
//        {
//            return error("用户ID不能为空");
//        }
//        return AjaxResult.success(users.put(user.getUserId(), user));

        return AjaxResult.error("保存失败：服务器收到的数据="+obj);
    }

    @ApiOperation("燕茹测试保存需求问卷信息")
//    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path", dataTypeClass = Integer.class)
//    @GetMapping("/{userId}")
    @PostMapping("/saveQuestionEntity")
    @ResponseBody
//    @RequestBody
//    @RequestMapping(value = "/saveQuestion")//, method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public AjaxResult saveQuestionEntity(@RequestBody CyQuestionRequirement jsonObject)//JSONObject jsonObject)
    {

        Date date = new Date();
//        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");// :hh:mm:ss");
//        System.out.println(dateFormat.format(date));
        jsonObject.setSubmitTime(date);//(date));
        //@RequestBody String jsonObject)//
        logger.debug("收到需求问卷数据保存实体："+jsonObject);
//        JSONObject json = JSONObject.parseObject(jsonObject);
        //JSONObject jo = JSONObject.parseObject(jsonObject);
//        logger.debug("收到用户数据2："+jsonObject);

        //保存数据
        try{
            this.cyQuestionRequirementService.insertCyQuestionRequirement(jsonObject);
        }catch (Exception ex)
        {
            logger.error("保存失败ex="+ex.getMessage()+"@");
            //Data too long for column 'dept_name'
            String errorMessage = "保存失败:";
            if (ex.getMessage().indexOf("Data too long for column" )!= -1) {
                if(ex.getMessage().indexOf("dept_name") != -1)
                {
                    errorMessage += "部门过长";
                }
                else if(ex.getMessage().indexOf("post_name")!=-1){
                    errorMessage += "岗位过长";
                }
                else {
                    errorMessage += "字段信息过长";
                }
            }
            else {
                errorMessage = "保存失败,请联系管理员";
            }
            return AjaxResult.error(errorMessage);
        }

        return AjaxResult.success("提交成功，感谢您的反馈！");
    }

    //
    @ApiOperation("燕茹测试保存售后问卷")
//    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path", dataTypeClass = Integer.class)
//    @GetMapping("/{userId}")
    @PostMapping("/saveAfterinvestigationEntity")
    @ResponseBody
//    @RequestBody
//    @RequestMapping(value = "/saveQuestion")//, method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public AjaxResult saveAfterinvestigationEntity(@RequestBody CyAfterinvestigation jsonObject)//JSONObject jsonObject)
    {

        Date date = new Date();
//        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");// :hh:mm:ss");
//        System.out.println(dateFormat.format(date));
        jsonObject.setSubmitTime(date);//(date));
        //@RequestBody String jsonObject)//
        logger.debug("收到售后用户数据保存实体："+jsonObject);
//        JSONObject json = JSONObject.parseObject(jsonObject);
        //JSONObject jo = JSONObject.parseObject(jsonObject);
//        logger.debug("收到用户数据2："+jsonObject);

        //保存数据
        try{
            this.cyAfterinvestigationService.insertCyAfterinvestigation(jsonObject);
        }catch (Exception ex)
        {
            logger.error("保存失败ex="+ex.getMessage()+"@");
            //Data too long for column 'dept_name'
            String errorMessage = "保存失败:";
            if (ex.getMessage().indexOf("Data too long for column" )!= -1) {
                if(ex.getMessage().indexOf("dept_name") != -1)
                {
                    errorMessage += "部门过长";
                }
                else if(ex.getMessage().indexOf("post_name")!=-1){
                    errorMessage += "岗位过长";
                }
                else {
                    errorMessage += "字段信息过长";
                }
            }
            else {
                errorMessage = "保存失败,请联系管理员";
            }
            return AjaxResult.error(errorMessage);
        }

        return AjaxResult.success("提交成功，感谢您的反馈！");
    }

//    @ApiOperation("新增用户")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "Integer", dataTypeClass = Integer.class),
//            @ApiImplicitParam(name = "username", value = "用户名称", dataType = "String", dataTypeClass = String.class),
//            @ApiImplicitParam(name = "password", value = "用户密码", dataType = "String", dataTypeClass = String.class),
//            @ApiImplicitParam(name = "mobile", value = "用户手机", dataType = "String", dataTypeClass = String.class)
//    })
//    @PostMapping("/save")
//    public AjaxResult save(UserEntity user)
//    {
//        if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId()))
//        {
//            return error("用户ID不能为空");
//        }
//        return AjaxResult.success(users.put(user.getUserId(), user));
//    }
//
//    @ApiOperation("更新用户")
//    @PutMapping("/update")
//    public AjaxResult update(@RequestBody UserEntity user)
//    {
//        if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId()))
//        {
//            return error("用户ID不能为空");
//        }
//        if (users.isEmpty() || !users.containsKey(user.getUserId()))
//        {
//            return error("用户不存在");
//        }
//        users.remove(user.getUserId());
//        return AjaxResult.success(users.put(user.getUserId(), user));
//    }
//
//    @ApiOperation("删除用户信息")
//    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path", dataTypeClass = Integer.class)
//    @DeleteMapping("/{userId}")
//    public AjaxResult delete(@PathVariable Integer userId)
//    {
//        if (!users.isEmpty() && users.containsKey(userId))
//        {
//            users.remove(userId);
//            return success();
//        }
//        else
//        {
//            return error("用户不存在");
//        }
//    }
}
